package com.ezia.coreservices.charging.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ezia.coreservices.charging.dao.MaStationInfoDao;
import com.ezia.coreservices.charging.service.ChargingLocationSearchService;
import com.ezia.coreservices.charging.utils.ResultMsg;
import com.ezia.coreservices.charging.utils.ResultStatusCode;

import net.sf.json.JSONObject;

@Service
public class ChargingLocationSearchServiceImpl implements ChargingLocationSearchService {
	
	private static final Logger logger = LoggerFactory.getLogger(ChargingLocationSearchServiceImpl.class);
	
	@Autowired
	private MaStationInfoDao stationInfoDao;
	
	@Override
	public ResultMsg findByPOIName(String keyword, String areaCode, Integer pageSize, Integer pageNo, String scope,
			String filter, Integer coordType, Map<String, Object> userInfo) {
		//声明查询map
		Map<String,Object> queryMap = new HashMap<String,Object>();
		//判断userInfo中必填字段
		if(!userInfo.containsKey("idType")||!userInfo.containsKey("idValue")||!userInfo.containsKey("longitude")||!userInfo.containsKey("latitude")) {
			return new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(), ResultStatusCode.Parameter_Error.getRespMsg(),null);
		}
		//获取车辆用户经纬度
		queryMap.put("userlongitude", userInfo.get("longitude"));
		queryMap.put("userlatitude", userInfo.get("latitude"));
		//添加查询关键字，区域码 coordType默认为2 不处理
		queryMap.put("keyword",keyword );
		queryMap.put("areaCode",areaCode);
		//判断是否含有分页参数，如果有一页最大不可超过50条，如果没有则默认查十条
		if(null!=pageSize&&null!=pageNo) {
			if(pageSize>50) {
				pageSize = 50;
			}
			queryMap.put("offset", (pageNo-1)*pageSize);
			queryMap.put("limit", pageSize);
		}
		else {
			pageSize = 10;
			queryMap.put("offset", 0);
			queryMap.put("limit", 10);
		}
		//处理过滤条件
		if(null!=filter&&!filter.equals("")) {
			String[] filterArray = filter.split(",");
			for(int i =0;i<filterArray.length;i++) {
				if(filterArray[i].indexOf("Sort")!=-1) {
					String[] orderArray =filterArray[i].toString().split(":");
					queryMap.put("Sort", orderArray[1]);
					logger.info("Sort:"+orderArray[1]);
				}
				if(filterArray[i].indexOf("stationType")!=-1) {
					String[] orderArray =filterArray[i].toString().split(":");
					queryMap.put("stationType", orderArray[1]);
					logger.info("stationType:"+orderArray[1]);
				}
				if(filterArray[i].indexOf("chargeMode")!=-1) {
					String[] orderArray =filterArray[i].toString().split(":");
					queryMap.put("chargeMode", orderArray[1]);
					logger.info("chargeMode:"+orderArray[1]);
				}
				if(filterArray[i].indexOf("CP_Status")!=-1) {
					String[] orderArray =filterArray[i].toString().split(":");
					queryMap.put("CP_Status", orderArray[1]);
					logger.info("CP_Status:"+orderArray[1]);
				}
			}
		}
		//构建返回结果
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationinfo = new ArrayList<Map<String, Object>>();
		stationinfo = stationInfoDao.queryStation(queryMap);
		int total = stationInfoDao.queryStationTotal(queryMap);
		int pageCount =(int)Math.ceil((double)total/pageSize);
		result.put("total", total);
		result.put("pageCount ", pageCount);
		result.put("pageNo", pageNo);
		String stationId = null;
		//根据scope判断是否为详细查询
		if(scope.equals("1")) {//基本查询
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				//查询交流直流桩总数，可用，不可用
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
			}
			result.put("stationinfos", stationinfo);	
			return new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(),result);
		}else {//详细查询
			for(int i = 0;i<stationinfo.size();i++) 
			{
				stationId = (String)stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				Map<String, Object> detailMap = new LinkedHashMap<String, Object>();
				detailMap = stationInfoDao.queryStationDetailInfo(stationId);
				List<Map<String, Object>> equipmentList = stationInfoDao.queryEquipmentInfoById(stationId);				
				for(int m =0;m<equipmentList.size();m++) 
				{
					String equipmentId = (String)equipmentList.get(m).get("EquipmentID");
					equipmentList.get(m).put("ConnectorInfos", stationInfoDao.queryConnectorInfoById(equipmentId));
				}
				detailMap.put("equplist", equipmentList);
				stationinfo.get(i).put("detail_info",detailMap);
				
				
			}
			result.put("stationinfos", stationinfo);
			return new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(),result);
		}
	}

	@Override
	public ResultMsg findByLocation(String scope, String filter, Integer coordType, Integer pageSize, Integer pageNo,
			String latitude, String longitude, String radius, Map<String, Object> userInfo) {
		//声明查询map
		Map<String, Object> queryMap = new HashMap<String, Object>();
		// 判断userInfo中必填字段
		if (!userInfo.containsKey("idType") || !userInfo.containsKey("idValue") || !userInfo.containsKey("longitude")
				|| !userInfo.containsKey("latitude")) {
			return new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(),
					ResultStatusCode.Parameter_Error.getRespMsg(), null);
		}
		// 获取车辆用户经纬度
		queryMap.put("userlongitude", userInfo.get("longitude"));
		queryMap.put("userlatitude", userInfo.get("latitude"));
		// 添加查询关键字，区域码 coordType默认为2 不处理
		queryMap.put("latitude", latitude);
		queryMap.put("longitude", longitude);
		//radius取值0-10
		if(Integer.parseInt(radius)>10) {
			radius= "10";
		}else {
			queryMap.put("radius", radius);
		}
		queryMap.put("radius", radius);
		// 判断是否含有分页参数，如果有一页最大不可超过50条，如果没有则默认查十条
		if (null != pageSize && null != pageNo) {
			if (pageSize > 50) {
				pageSize = 50;
			}
			queryMap.put("offset", (pageNo - 1) * pageSize);
			queryMap.put("limit", 50);
		} else {
			pageSize = 10;
			queryMap.put("offset", 0);
			queryMap.put("limit", 10);
		}
		// 处理过滤条件
		if (null != filter && !filter.equals("")) {
			String[] filterArray = filter.split(",");
			for (int i = 0; i < filterArray.length; i++) {
				if (filterArray[i].indexOf("Sort") != -1) {
					String[] orderArray = filterArray[i].toString().split(":");
					queryMap.put("Sort", orderArray[1]);
					logger.info("Sort:" + orderArray[1]);
				}
				if (filterArray[i].indexOf("cps_type") != -1) {
					String[] orderArray = filterArray[i].toString().split(":");
					queryMap.put("cps_type", orderArray[1]);
					logger.info("cps_type:" + orderArray[1]);
				}
				if (filterArray[i].indexOf("chargeMode") != -1) {
					String[] orderArray = filterArray[i].toString().split(":");
					queryMap.put("chargeMode", orderArray[1]);
					logger.info("chargeMode" + orderArray[1]);
				}
				if (filterArray[i].indexOf("CP_Status") != -1) {
					String[] orderArray = filterArray[i].toString().split(":");
					queryMap.put("CP_Status", orderArray[1]);
					logger.info("CP_Status:" + orderArray[1]);
				}
				if (filterArray[i].indexOf("bookable") != -1) {
					String[] orderArray = filterArray[i].toString().split(":");
					queryMap.put("bookable", orderArray[1]);
					logger.info("bookable:" + orderArray[1]);
				}
			}
		}
		// 构建返回结果
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationinfo = new ArrayList<Map<String, Object>>();
		stationinfo = stationInfoDao.queryStation(queryMap);
		int total = stationInfoDao.queryStationTotal(queryMap);
		int pageCount =(int)Math.ceil((double)total/pageSize);
		result.put("total", total);
		result.put("pageCount ", pageCount);
		result.put("pageNo", pageNo);
		String stationId = null;
		// 根据scope判断是否为详细查询
		if (scope.equals("1")) {// 基本查询
			for (int i = 0; i < stationinfo.size(); i++) {
				stationId = (String) stationinfo.get(i).get("stationId");
				//查询交流直流桩总数，可用，不可用
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
			}
			result.put("stationinfos", stationinfo);
			return new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(), result);
		} else {// 详细查询
			for (int i = 0; i < stationinfo.size(); i++) {
				stationId = (String) stationinfo.get(i).get("stationId");
				stationinfo.get(i).put("ACTotalNumber", stationInfoDao.queryACTotalNumber(stationId));
				stationinfo.get(i).put("ACAvailableNumber", stationInfoDao.queryACAvailableNumber(stationId));
				stationinfo.get(i).put("DCTotalNumber", stationInfoDao.queryDCTotalNumber(stationId));
				stationinfo.get(i).put("DCAvailableNumber", stationInfoDao.queryDCAvailableNumber(stationId));
				Map<String, Object> detailMap = new LinkedHashMap<String, Object>();
				detailMap = stationInfoDao.queryStationDetailInfo(stationId);
				List<Map<String, Object>> equipmentList = stationInfoDao.queryEquipmentInfoById(stationId);
				for (int m = 0; m < equipmentList.size(); m++) {
					String equipmentId = (String) equipmentList.get(m).get("EquipmentID");
					equipmentList.get(m).put("ConnectorInfos", stationInfoDao.queryConnectorInfoById(equipmentId));
				}
				detailMap.put("equplist", equipmentList);
				stationinfo.get(i).put("detail_info", detailMap);

			}
			result.put("stationinfos", stationinfo);
			return new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(), result);
		}
	}

	@Override
	public ResultMsg getDynamicInfo(String CPO_id, String stationId, Map<String, Object> userInfo) {
		// 判断userInfo中必填字段
		if (!userInfo.containsKey("idType") || !userInfo.containsKey("idValue") || !userInfo.containsKey("longitude")
				|| !userInfo.containsKey("latitude")) {
			return new ResultMsg(ResultStatusCode.Parameter_Error.getRespCode(),
					ResultStatusCode.Parameter_Error.getRespMsg(), null);
		}
		// 构建返回结果
		Map<String, Object> result = new LinkedHashMap<String, Object>();
		List<Map<String, Object>> stationStatusInfo = stationInfoDao.queryStationById(stationId);
		for (int i = 0; i < stationStatusInfo.size(); i++) {
			String queryStationId = (String) stationStatusInfo.get(i).get("StationID");
			List<Map<String, Object>> statusList = stationInfoDao.queryConnectorStatusInfoById(queryStationId);
			stationStatusInfo.get(i).put("ConnectorStatusInfos", statusList);
		}
		result.put("stationStatusInfo", stationStatusInfo);
		return new ResultMsg(ResultStatusCode.Success.getRespCode(), ResultStatusCode.Success.getRespMsg(), result);
	}

}
